package com.child.java.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.child.java.page.PageInfo;
import com.child.java.pojo.ChildWearVO;
import com.child.java.service.ChildWearService;

@Controller
public class ChildWearController {

	@Autowired
	ChildWearService childWearService;
	
	@RequestMapping("/childWear/listAll")
	public String list(ModelMap map,HttpServletRequest request){
		if(request.getParameter("urlControl")!=null){
			String urlContorl = request.getParameter("urlControl");
			map.put("urlControl", urlContorl);
		}
		String wearCode = request.getParameter("wearCode");
		Integer pageNo = 1;
		if(request.getParameter("pageNo")!=null&&request.getParameter("pageNo").length()>0) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("wearCode", wearCode);
		int totalRecord = childWearService.getListCount(m);
		PageInfo<ChildWearVO> pageInfo = new PageInfo<ChildWearVO>(totalRecord, 10, pageNo);
		m.put("limitIndex", (pageNo-1)*pageInfo.getPageSize());
		m.put("pageSize", pageInfo.getPageSize());
		
		List<ChildWearVO> list = childWearService.getListByPageInfo(m);
		map.put("list", list);
		map.put("pageInfo", pageInfo);
		return "/manager/childWear/childWearList";
	}
	
	@RequestMapping("/childWear/showAdd")
	public String showAdd(ModelMap map){
		return "/manager/childWear/childWearEdit";
	}
	
	@RequestMapping("/childWear/showUpdate")
	public String showUpdate(@RequestParam("id") String id,ModelMap map){
		ChildWearVO childWear = childWearService.getById(id);
		map.put("childWear", childWear);
		return "/manager/childWear/childWearEdit";
	}

	@RequestMapping("/childWear/save")
	public String add(HttpServletRequest request,ModelMap map){
		ChildWearVO childWear = new ChildWearVO();
		childWear.setId(request.getParameter("id"));
		childWear.setWearCode(request.getParameter("wearCode"));
		childWear.setWearType(request.getParameter("wearType"));
		childWear.setInPrice(Double.parseDouble(request.getParameter("inPrice")));
		childWear.setOutPrice(Double.parseDouble(request.getParameter("outPrice")));
		childWear.setInventory(Integer.parseInt(request.getParameter("inventory")));
		childWear.setSales(Integer.parseInt(request.getParameter("sales")));
		
		if(childWear.getId()!=null&&childWear.getId().length()>0) {
			childWearService.update(childWear);
		}else {
			childWearService.insert(childWear,request.getParameter("wearSize"));
		}
		return "redirect:/childWear/listAll";
	}
	
	@RequestMapping("/childWear/delete")
	public String add(@RequestParam("id") String id,ModelMap map){
		childWearService.delete(id);
		return "redirect:/childWear/listAll";
	}
	
}
